﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using RestService.Processing;
using System.Collections;
using System.Data;

namespace RestService.Processing
{
    public class Point:DoiTuong
    {
        #region decimal X property
        public decimal X
        {
            get
            {
                if (_DataRow["X"] is decimal) return (decimal)_DataRow["X"];
                return 0;
            }
            set
            {
                _DataRow["X"] = (decimal)value;
            }
        }
        #endregion
        #region decimal Y property
        public decimal Y
        {
            get
            {
                if (_DataRow["Y"] is decimal) return (decimal)_DataRow["Y"];
                return 0;
            }
            set
            {
                _DataRow["Y"] = (decimal)value;
            }
        }
        #endregion
        #region string Direct property
        public string Direct
        {
            get
            {
                if (_DataRow["Direct"] is string) return (string)_DataRow["Direct"];
                return "";
            }
            set
            {
                _DataRow["Direct"] = (string)value;
            }
        }
        #endregion
        #region decimal Degree property
        public decimal Degree
        {
            get
            {
                if (_DataRow["Degree"] is decimal) return (decimal)_DataRow["Degree"];
                return 0;
            }
            set
            {
                _DataRow["Degree"] = (decimal)value;
            }
        }
        #endregion

        public Point(DataRow dataRow)
            : base(dataRow)
        {
            DanhMucPoint dm = new DanhMucPoint();
            if (this.ID <= 0) this.ID = dm.GetNextID();
        }
        public int GetMaxID()
        {
            DanhMucPoint dm = new DanhMucPoint();
            return dm.GetNextID();
        }
    }

    public class DanhMucPoint : DanhMucDoiTuong, IEnumerable {
        #region Instance property: Singleton Design pattern
        private static DanhMucPoint _Instance;
        public static DanhMucPoint Instance
        {
            get
            {
                if (_Instance == null)
                {
                    _Instance = new DanhMucPoint();
                    _Instance.DataView = _Instance.DataTable.DefaultView;
                }
                else _Instance.LoadFromDB();
                return _Instance;
            }
        }
        #endregion

        #region override TableName property
        public override string TableName
        {
            get
            {
                return "Point";
            }
        }
        #endregion

        #region Methods required by IEnumerable (use for foreach)
        public IEnumerator GetEnumerator()
        {
            return new TokenEnumerator(this);
        }

        private class TokenEnumerator : IEnumerator
        {
            private int _Position = -1;
            DanhMucDoiTuong _Dstk;
            private DanhMucPoint danhMucPoint;

            public TokenEnumerator(DanhMucDoiTuong dstk)
            {
                _Dstk = dstk;
            }

            public TokenEnumerator(DanhMucPoint danhMucPoint)
            {
                // TODO: Complete member initialization
                this.danhMucPoint = danhMucPoint;
            }

            // Declare the MoveNext method required by IEnumerator:
            public bool MoveNext()
            {
                if (_Position < _Dstk.DataView.Count - 1)
                {
                    _Position++;
                    return true;
                }
                else
                {
                    return false;
                }
            }

            // Declare the Reset method required by IEnumerator:
            public void Reset()
            {
                _Position = -1;
            }

            // Declare the Current property required by IEnumerator:
            public object Current
            {
                get
                {
                    return new AP((_Dstk.DataView[_Position].Row));
                }
            }
        }
        #endregion

        public Point this[int index]
        {
            get
            {
                if (0 <= index && index <= this.Count)
                {
                    return new Point(DataTable.Rows[index]);
                }
                else
                {
                    return null;
                }
            }
        }

        public Point NewDoiTuong()
        {
            return new Point(DataTable.NewRow());
        }

        public Point GetByProperty(string property, object value)
        {
            DataRow[] rows = GetDataRows(property, value);
            if (rows.Length > 0) return new Point(rows[0]);
            else return null;
        }

        public Point GetByID(int objID)
        {
            return GetByProperty("PID", objID);
        }

        public static DanhMucPoint GetAll()
        {
            return Instance;
        }

        
        public static DanhMucPoint Filter(string filterExpression)
        {
            try
            {
                DanhMucPoint obj = new DanhMucPoint();
                obj.DataView = new DataView(DanhMucPoint.Instance.DataTable, filterExpression, "", DataViewRowState.CurrentRows);
                return obj;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
    }
}